package com.coke.spring.seata.controller;


import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.coke.spring.seata.entity.User;
import org.apache.lucene.search.TotalHits;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

@RestController
public class TestController {
    @Autowired
    private RestHighLevelClient restHighLevelClient;

    private static final String INDEX = "user";
    private final static List<User> map = new ArrayList() {
        {
            add(new User(1, "张三",null, "2021-08-22 16:30:46"));
             add(new User(2,"李四",null,"2021-07-22 16:30:46"));
            add(new User(3,"王五",null,"2021-06-22 16:30:46"));
            add(new User(4,"赵六",null,"2021-05-22 16:30:46"));
            add(new User(5,"陈八",null,"2021-04-22 16:30:46"));
        }
    };

    @PutMapping("/add")
    public void add() throws IOException {
        BulkRequest
                bulkRequest = new BulkRequest();
        for (User user : map
        ) {
            IndexRequest indexRequest = new IndexRequest(INDEX);
            indexRequest.id(user.getId().toString());
            String s = JSONUtil.toJsonStr(user);
            JSONObject jsonObject = JSONUtil.parseObj(user);
            indexRequest.source(jsonObject, XContentType.JSON);
            bulkRequest.add(indexRequest);
        }
        BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
        System.out.println();
    }

    @GetMapping("/get")
    public void get() throws IOException {
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices("user");
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder.must(QueryBuilders.termQuery("id", "1"));
        SearchSourceBuilder query = sourceBuilder.query(boolQueryBuilder);
        searchRequest.source(query);
        SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        SearchHits hits = search.getHits();
        SearchHit at = hits.getAt(0);
        String sourceAsString = at.getSourceAsString();
        User user = JSONUtil.toBean(JSONUtil.parseObj(sourceAsString), User.class);
        System.out.println();
    }

    public static Date getDaDate() {
        SimpleDateFormat
                sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //设置为东八区
        sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        Date date = new Date();
        String dateStr = sdf.format(date);

        //将字符串转成时间
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date newDate = null;
        try {
            newDate = df.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return newDate;
    }


}
